﻿using System;
using System.Runtime.InteropServices;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Hutchinson.CustomField.LookupField
{
    // TODO: Replace, as needed, "TextField" with some other class derived from Microsoft.SharePoint.WebControls.BaseFieldControl.
    [CLSCompliant(false)]
    [Guid("41948454-E50D-4d59-9F46-11468B38BDF5")]
    public class HutLookupFieldControl : HutLookupFieldControlBase
    {
        #region Variables

        protected DropDownList SelectCandidate;
        //protected HutLookupFieldField CurrentHutchinsonLookupFieldField = null;

        #endregion

        #region Propriétés

        /// <summary>
        /// Retourne le SPFieldLookupValue de la valeur selectionnée dans la liste (Id)
        /// </summary>
        public override object Value
        {
            get
            {
                EnsureChildControls();
                if (SelectCandidate.SelectedValue != "")
                    return new SPFieldLookupValue(SelectCandidate.SelectedValue);
                else
                    return "";

            }
        }

        #endregion

        /// <summary>
        /// Définition des contrôles utilisés par le Custom FIeld
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            try
            {
                if (this.ControlMode == SPControlMode.Edit || this.ControlMode == SPControlMode.New)
                {

                    //Récupération des contrôles
                    SelectCandidate = TemplateContainer.FindControl("SelectCandidate") as DropDownList;

                    CurrentHutchinsonLookupFieldField = Field as HutLookupFieldField;

                    if (SelectCandidate == null)
                        throw new ApplicationException(CurrentHutchinsonLookupFieldField.GetRessources("Error_ControlInstanciation"));

                    SelectCandidate.Attributes.Add("FieldInternalName", Field.InternalName);

                    //Initialisation de la DropDownList
                    //if (!CurrentHutchinsonLookupFieldField.Required)
                        SelectCandidate.Items.Add("");
                    SPField fieldParametred = liste.Fields[new Guid(CurrentHutchinsonLookupFieldField.LookupFieldParametred)];

                    if (CurrentHutchinsonLookupFieldField.Required && 
                        CurrentHutchinsonLookupFieldField.DefaultValue != null)
                    {
                        SPListItem element = liste.GetItemById(int.Parse(CurrentHutchinsonLookupFieldField.DefaultValue));
                        ListItem elementListe = new ListItem();
                        elementListe.Text = HutLookupFieldField.cleanValue(element[fieldParametred.Id].ToString());
                        elementListe.Value = element.ID.ToString();
                        if (!string.IsNullOrEmpty(elementListe.Text))
                            SelectCandidate.Items.Add(elementListe);

                        SelectCandidate.SelectedValue = CurrentHutchinsonLookupFieldField.DefaultValue;
                    }
                    else
                    {
                        SPListItemCollection elements = GetItemCollection(fieldParametred, liste);
                        foreach (SPListItem element in elements)
                        {
                            ListItem elementListe = new ListItem();
                            elementListe.Text = HutLookupFieldField.cleanValue(element[fieldParametred.Id].ToString());
                            elementListe.Value = element.ID.ToString();
                            if (!string.IsNullOrEmpty(elementListe.Text))
                                SelectCandidate.Items.Add(elementListe);
                        }
                        if (base.ItemFieldValue != null)
                        {
                            if (base.ItemFieldValue is SPFieldLookupValue)
                            {
                                SPFieldLookupValue value2Select = (SPFieldLookupValue)base.ItemFieldValue;
                                SelectCandidate.SelectedValue = value2Select.LookupId.ToString();
                            }
                            else
                            {
                                SelectCandidate.SelectedValue = base.ItemFieldValue.ToString();
                            }
                        }
                        else if (CurrentHutchinsonLookupFieldField.DefaultValue != null)
                        {
                            SelectCandidate.SelectedValue = CurrentHutchinsonLookupFieldField.DefaultValue;
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                base.ErrorManage(Ex.Message);
            }

        }
    }
}
